using Abp.Dependency;
using Abp.Runtime.Caching.Configuration;

namespace Abp.Runtime.Caching.Redis;

/// <summary>
/// Used to create <see cref="AbpPerRequestRedisCache"/> instances.
/// </summary>
public class AbpPerRequestRedisCacheManager : CacheManagerBase<ICache>, IAbpPerRequestRedisCacheManager
{
    private readonly IIocManager _iocManager;

    /// <summary>
    /// Initializes a new instance of the <see cref="AbpPerRequestRedisCacheManager"/> class.
    /// </summary>
    public AbpPerRequestRedisCacheManager(IIocManager iocManager, ICachingConfiguration configuration)
        : base(configuration)
    {
        _iocManager = iocManager;
        _iocManager.RegisterIfNot<AbpPerRequestRedisCache>(DependencyLifeStyle.Transient);
    }

    protected override ICache CreateCacheImplementation(string name)
    {
        return _iocManager.Resolve<AbpPerRequestRedisCache>(new { name });
    }

    protected override void DisposeCaches()
    {
        foreach (var cache in Caches)
        {
            _iocManager.Release(cache.Value);
        }
    }
}

internal class AbpPerRequestRedisCacheManagerForReplacement : AbpPerRequestRedisCacheManager
{
    public AbpPerRequestRedisCacheManagerForReplacement(IIocManager iocManager, ICachingConfiguration configuration) : base(iocManager, configuration)
    {
    }
}